package com.guli.mall.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.common.to.SpuAndSkuListTo;
import com.guli.common.valid.AddGroup;
import com.guli.mall.pms.entity.vo.SkuInfoWithMinMaxPriceVo;
import com.guli.mall.pms.entity.vo.SpuInfoImgVo;
import com.guli.mall.pms.entity.vo.SpuInfoWithNameVo;
import com.guli.mall.pms.entity.vo.productSpuAdd.JsonRootBean;
import com.guli.mall.pms.entity.vo.productSpuAdd.SpuInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.pms.entity.SpuInfoEntity;
import com.guli.mall.pms.service.SpuInfoService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * spu信息
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-17 11:37:22
 */
@RestController
@RequestMapping("pms/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取spuInfo分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size,@RequestParam(required = false,value = "spuId") Long spuId) {
        PageUtils page = spuInfoService.queryPage(currentPage, size,spuId);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取SpuInfo")
    public R info(@PathVariable("id") Long id) {

        SpuInfoImgVo spuInfo = spuInfoService.getSpuInfoImgById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    @PostMapping("/save")
    @ApiOperation("插入SpuInfo")
    public R save(@RequestBody @Validated(AddGroup.class) SpuInfoImgVo spuInfo) {
        spuInfoService.saveInfoDescAndImg(spuInfo);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新SpuInfo")
    public R update(@RequestBody SpuInfoEntity spuInfo) {

        spuInfoService.updateByIdAndWriteToEs(spuInfo);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除SpuInfo")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/product/spuAdd")
    @ApiOperation("插入一个完整的商品")
    public R spuAdd(@RequestBody JsonRootBean jsonRootBean){

        spuInfoService.spuAdd(jsonRootBean);

        return R.ok();
    }

    @PostMapping("/searchSpuByCondition/{currentIndex}/{size}")
    @ApiOperation("条件搜索spu")
    public R searchSpuByCondition(@RequestBody SpuInfoEntity spuInfoEntity,
                                  @PathVariable("currentIndex")Long currentIndex,
                                  @PathVariable("size")Integer size){
        PageUtils<SpuInfoWithNameVo> page = this.spuInfoService.searchSpuByCondition(spuInfoEntity,currentIndex,size);
        return R.ok().put("page",page);
    }

    @PostMapping("/getSpuAndSkuBySpuId")
    @ApiOperation("根据spuIdList获取封装好了的spu以及它的sku")
    public Map<Long,SpuAndSkuListTo> getSpuAndSkuBySpuId(@RequestBody List<Long> spuIds){

        return spuInfoService.getSpuAndSkuBySpuId(spuIds);
    }

}
